package trafficAgent;

import model.Car;
import model.Config;
import jade.core.behaviours.OneShotBehaviour;

import java.util.ArrayList;
import java.util.List;

public class RemoveGoalCars extends OneShotBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	TrafficAgent agent; 
	
	public RemoveGoalCars(final TrafficAgent myAgent) {
		super(myAgent);
		agent = myAgent;
	}

	@Override
	public void action() {
		
		
		
		List<Car> toRemove = new ArrayList<Car>();
		for(Car c: agent.getMap().getCars()){
			if(c.getCurPos().equals(c.getDestPos()) && c.getCurPosIndex() == c.getDestPosIndex())
				toRemove.add(c);
			if(c.getCurPos().getStart().equals(c.getDestPos().getEnd()) && c.getCurPos().getEnd().equals(c.getDestPos().getStart()) && c.getCurPosIndex() == Config.nCellsPalace - 1 - c.getDestPosIndex())
				toRemove.add(c);
		}
		
		for(Car c: toRemove){
			agent.addMenagedCar();
			agent.addIdleTime(c.getIdleTime());
			agent.addWaitingTime(c.getWaitingTime());
			agent.getMap().getCars().remove(c);
		}
		
		agent.addBehaviour(new MasterBehaviour(agent));
	}

}
